package apis

import (
	"fmt"
	"goshop/core/plugin/netlib"
	urlNet "net/url"
	"strings"
	"time"
)

//get 请求 args[0]:map[string]string, args[1]:time.Duration
func Get(uri string, args ...interface{}) *netlib.NykjHTTPRequest {
	data := func(args ...interface{}) *map[string]interface{} {
		var d = map[string]interface{}{}
		if len(args) > 0 {
			d = args[0].(map[string]interface{})
		}
		return &d
	}(args)
	if len(*data) > 0 {
		//参数 url-encode 处理
		queryStr := urlNet.Values{}
		for k, v := range *data {
			queryStr.Add(k, fmt.Sprintf("%v", v))
		}
		if strings.Contains(uri, "?") {
			uri += "&" + queryStr.Encode()
		} else {
			uri += "?" + queryStr.Encode()
		}
	}
	req := netlib.Get(uri).SetTimeout(netlib.DEFAULT_CONNECT_TIME, 3*time.Second)
	return req
}

//post 请求 args[0]:map[string]string, args[1]:time.Duration
func PostForm(url string, args ...interface{}) *netlib.NykjHTTPRequest {
	req := netlib.Post(url).SetTimeout(netlib.DEFAULT_CONNECT_TIME, 3*time.Second)
	data := func(args ...interface{}) *map[string]interface{} {
		var d = map[string]interface{}{}
		if len(args) > 0 {
			d = args[0].(map[string]interface{})
		}
		return &d
	}(args)
	if len(*data) > 0 {
		for k, v := range *data {
			req.Param(k, fmt.Sprintf("%v", v))
		}
	}
	return req
}

//post 请求 args[0]:map[string]string, args[1]:time.Duration
func PostRaw(url string, args ...interface{}) *netlib.NykjHTTPRequest {
	req := netlib.Post(url).SetTimeout(netlib.DEFAULT_CONNECT_TIME, 3*time.Second)
	data := func(args ...interface{}) *map[string]interface{} {
		var d = map[string]interface{}{}
		if len(args) > 0 {
			d = args[0].(map[string]interface{})
		}
		return &d
	}(args)
	req.JSONBody(data)
	return req
}

//post 请求 args[0]:map[string]string, args[1]:time.Duration
func Put(url string, args ...interface{}) *netlib.NykjHTTPRequest {
	req := netlib.Put(url).SetTimeout(netlib.DEFAULT_CONNECT_TIME, 3*time.Second)
	data := func(args ...interface{}) *map[string]interface{} {
		var d = map[string]interface{}{}
		if len(args) > 0 {
			d = args[0].(map[string]interface{})
		}
		return &d
	}(args)
	req.JSONBody(data)
	return req
}

//patch 请求 args[0]:map[string]string, args[1]:time.Duration
func Patch(url string, args ...interface{}) *netlib.NykjHTTPRequest {
	req := netlib.Patch(url).SetTimeout(netlib.DEFAULT_CONNECT_TIME, 3*time.Second)
	data := func(args ...interface{}) *map[string]interface{} {
		var d = map[string]interface{}{}
		if len(args) > 0 {
			d = args[0].(map[string]interface{})
		}
		return &d
	}(args)
	req.JSONBody(data)
	return req
}

//post 请求 args[0]:map[string]string, args[1]:time.Duration
func Delete(url string, args ...interface{}) *netlib.NykjHTTPRequest {
	req := netlib.Delete(url).SetTimeout(netlib.DEFAULT_CONNECT_TIME, 3*time.Second)
	data := func(args ...interface{}) *map[string]interface{} {
		var d = map[string]interface{}{}
		if len(args) > 0 {
			d = args[0].(map[string]interface{})
		}
		return &d
	}(args)
	req.JSONBody(data)
	return req
}

//post 请求 args[0]:map[string]string, args[1]:time.Duration
func Head(url string, args ...interface{}) *netlib.NykjHTTPRequest {
	req := netlib.Head(url).SetTimeout(netlib.DEFAULT_CONNECT_TIME, 3*time.Second)
	data := func(args ...interface{}) *map[string]interface{} {
		var d = map[string]interface{}{}
		if len(args) > 0 {
			d = args[0].(map[string]interface{})
		}
		return &d
	}(args)
	req.JSONBody(data)
	return req
}
